// pages/find/find.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      tabs:[
        {
          id:0,
          value:"全部",
          isActive:true
        },
        {
          id:1,
          value:"学习",
          isActive:false
        },
        {
          id:2,
          value:"运动",
          isActive:false
        },
        {
          id:3,
          value:"出行",
          isActive:false
        },
        {
          id:4,
          value:"游戏",
          isActive:false
        }
      ],
      postList:[],
  }, 
  onShow(e){
    const res = [
      {
        id:0,
        value:"全部",
        isActive:true
      },
      {
        id:1,
        value:"学习",
        isActive:false
      },
      {
        id:2,
        value:"运动",
        isActive:false
      },
      {
        id:3,
        value:"出行",
        isActive:false
      },
      {
        id:4,
        value:"游戏",
        isActive:false
      }
    ];
    this.setData({
      tabs:res,
    })
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    const db = wx.cloud.database().collection("List");
    db.where({isok:false}).orderBy('data','desc').limit(10).get({
      success: function(res) {
        // res.data 是包含以上定义的两条记录的数组
        const templist = res.data;
        //console.log(res);
        that.setData(
          {
            postList:templist,
          }
        );
        wx.hideLoading({
          success: (res) => {},
        })
      } 
    })
  },/**/
  /**
   * 生命周期函数--监听页面加载
   */ 
  post(e)
  {
      wx.navigateTo({
        url: '../post/post'
      })
  },
   /**
   * 组件taps
   */
  handletapsitemchange(e){
      const {index}=e.detail;
      let {tabs}=this.data;
      tabs.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false);
      this.setData({tabs});
      wx.showLoading({
        title: '加载中',
      })
      const tab = tabs[e.detail.index].value;
      var that = this;
      const db = wx.cloud.database().collection("List");
      db.where(tab=='全部'?{isok:false}:{isok:false,name:tab}).orderBy('data','desc').limit(10).get({
        success: function(res) {
          // res.data 是包含以上定义的两条记录的数组
          const templist = res.data;
          //console.log(res);
          that.setData(
            {
              postList:templist,
            }
          );
          wx.hideLoading({
            success: (res) => {},
          })
        } 
      })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.onShow();
    wx.stopPullDownRefresh({
      success: (res) => {
        
      },
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log(1);
  },
})